package com.xbongbong.saas.model.impl;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.help.BaseExplainModelHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.WorkOrderExplainDao;
import com.xbongbong.saas.domain.entity.WorkOrderExplainEntity;
import com.xbongbong.saas.domain.entity.ext.WorkOrderExplainEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.WorkOrderExplainModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2019/3/12 11:05
 * @since v1.0
 * @version v1.0
 */
@Service("workOrderExplainModel")
public class WorkOrderExplainModelImpl extends BaseExplainModelHelp implements WorkOrderExplainModel {
    private static final String COLUMNS = " id, corpid, app_id, menu_id, form_id, explains, creator_id, add_time, update_time, del ";

    @Resource
    private WorkOrderExplainDao workOrderExplainDao;

    @Override
    public Integer insert(WorkOrderExplainEntity entity) throws XbbException {
        Long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return workOrderExplainDao.insert(entity);
    }

    @Override
    public Integer update(WorkOrderExplainEntity entity) throws XbbException {
        entity.setUpdateTime(DateTimeUtil.getInt());
        return workOrderExplainDao.update(entity);
    }

    @Override
    public List<WorkOrderExplainEntity> findEntitys(Map<String, Object> param) {

        List<WorkOrderExplainEntity> explainList = workOrderExplainDao.findEntitys(param);
        if (param.containsKey(StringConstant.CORPID)) {
            handleWorkOrderExplain(explainList, param.get(StringConstant.CORPID).toString());
        }
        return explainList;
    }

    @Override
    public List<WorkOrderExplainEntity> getNoHandleExplainsByFindEntitys(Map<String, Object> entity) {
        return workOrderExplainDao.findEntitys(entity);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return workOrderExplainDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer deleteByFormId(Long formId, String corpid) {
        return workOrderExplainDao.deleteByFormId(formId, corpid);
    }

    @Override
    public WorkOrderExplainEntityExt getByFormId(Long formId, String corpid) throws XbbException {
        WorkOrderExplainEntityExt workOrderExplainEntity = workOrderExplainDao.getByFormId(formId, corpid);
        if (Objects.nonNull(workOrderExplainEntity)) {
            String explain = workOrderExplainEntity.getExplains();
            if (Objects.equals("[]", explain)) {
                explain = handleFormExplain(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.WORK_ORDER.getCode(), explain, corpid, null);
            }
            workOrderExplainEntity = new WorkOrderExplainEntityExt(workOrderExplainEntity.getId(), workOrderExplainEntity.getCorpid(), workOrderExplainEntity.getAppId(), workOrderExplainEntity.getMenuId(),
                    workOrderExplainEntity.getFormId(), explain, workOrderExplainEntity.getCreatorId(), workOrderExplainEntity.getAddTime(), workOrderExplainEntity.getUpdateTime(),
                    workOrderExplainEntity.getDel(), JSON.parseArray(explain));
        }
        return workOrderExplainEntity;
    }

    @Override
    public WorkOrderExplainEntity getByKey(Long key, String corpid) throws XbbException {
        WorkOrderExplainEntity workOrderExplainEntity = workOrderExplainDao.getByKey(key, corpid);
        if (Objects.nonNull(workOrderExplainEntity)) {
            handleWorkOrderExplainSingle(workOrderExplainEntity, corpid);
        }
        return workOrderExplainEntity;
    }

    @Override
    public List<WorkOrderExplainEntity> list(Map<String, Object> param) {
        param.put("columns", COLUMNS);
        List<WorkOrderExplainEntity> explainList = workOrderExplainDao.findEntitys(param);
        if (param.containsKey(StringConstant.CORPID)) {
            handleWorkOrderExplain(explainList, param.get(StringConstant.CORPID).toString());
        }
        return explainList;
    }

    @Override
    public WorkOrderExplainEntity getDefaultAllListExplain(String corpid) {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", "0");
        param.put("del", 0);
        List<WorkOrderExplainEntity> entitys = findEntitys(param);
        handleWorkOrderExplain(entitys, corpid);
        return entitys.get(0);
    }

    @Override
    public List<WorkOrderExplainEntity> getByFormIdIn(Collection<Long> formIdIn, String corpid) {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("formIdIn", formIdIn);
        param.put("del", 0);
        List<WorkOrderExplainEntity> entitys = findEntitys(param);
        handleWorkOrderExplain(entitys, corpid);
        return entitys;
    }
    /**
     * 批量插入实体
     * @param list
     * @return 返回数据的列表
     */
    @Override
    public List<WorkOrderExplainEntity> insertBatch(List<WorkOrderExplainEntity> list) {
        workOrderExplainDao.insertBatch(list);
        return list;
    }

    @Override
    public Integer updateBatch(List<WorkOrderExplainEntity> workOrderExplainEntityList, String corpid) {

        return workOrderExplainDao.updateBatch(workOrderExplainEntityList,corpid);
    }
    /**
     * 批量删除
     * @param list 主键id集合
     * @param corpid
     * @author xingxing.xiao
     */
    @Override
    public Integer deleteBatch(List<Long> list, String corpid) {
        return workOrderExplainDao.deleteBatch(list, corpid);
    }

    @Override
    public void updateBatchExplain(List<WorkOrderExplainEntity> updateList, String corpid) {
        workOrderExplainDao.updateBatchExplain(updateList,corpid);
    }

    @Override
    public void updateBatchExplainNoCorpid(List<WorkOrderExplainEntity> updateList) {
        workOrderExplainDao.updateBatchExplainNoCorpid(updateList);
    }
}
